﻿//interaction avec Word , mise à jour du treeview des données du modèle

var tree;
var codeDataTemplate = "";
var currentIdExplorer = 0;
var titre = "";
var tag = "";

//demarrer l'affichage
function start() {
    var ligne = window.location.search;
    //récupérer l'argument de la querystring
    ligne = ligne.replace("?", "");
    ligne = ligne.replace("code=", "");

    codeDataTemplate = ligne;
    
    initTreeView();
    refreshTree();
}

//créer l'arbre
function initTreeView() 
{
    //create the TreeView instance:
    tree = new YAHOO.widget.TreeView("treeDiv");

    tree.subscribe("labelClick", function (node) {
        var txt = "";
        for (prop in node) {
            txt += prop + "=" + eval("node." + prop) + ";"
        }

        // alert("one" + txt);
        this.titre = node.label;
        this.tag = node.data.myNodeId;

        currentIdExplorer++;
        window.status = "include" + currentIdExplorer + ";" + this.tag + ";" + this.titre + ";" + node.data.myValue ;

    });

}

//aller chercher les fils du noeud niveau 1
function loadNodeData(node, onCompleteCallback) 
{
    var id = node.data.id;
    if (id != null) {
        //aller chercher la suite si besoin
        if (node.children.length == 0) {
            refreshTemplateDatasTree(id, node);
            node.refresh();
            onCompleteCallback();
           
        }
    }
   
   
}
 


//Refresh de l'arbre
function refreshTree() {

//TODO récupérer le code
    var code = "facture";
    var sUrl = urlService + "/getDocumentDataTemplates";

    var postData = "code=" + codeDataTemplate;

    //ouvrir une requete
    var dsLocalArray = new YAHOO.util.XHRDataSource(sUrl);
    dsLocalArray.connMethodPost = true;

    dsLocalArray.responseType = YAHOO.util.XHRDataSource.TYPE_XML;
    dsLocalArray.responseSchema =
	{
	    resultNode: "DataTemplate",
	    fields: [{ key: "Code" }, { key: "Description"}]
	};
	
    //gestion de la réponse
    dsLocalArray.sendRequest(
	postData,
	{ success: function (req, res) {
	    var data = res.results;
	    afficherTreeView(data);
	}
	}
);



}

//Affichage des modeles ... dynamiquement on fera les elts
function afficherTreeView(data) {
       
    //get a reusable reference to the root node:
    var root = tree.getRoot();

    for (i = 0; i < data.length; i++) {
        var myobj = {
            label: data[i].Description,
            myNodeId: data[i].Code
        };

        var node = new YAHOO.widget.TextNode(myobj, root, false);
        node.data.id = data[i].Code;
        refreshTemplateDatasTree(node.data.id, node);
    }

    tree.draw();
    
}
